package com.sulj.domain;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 购物条目类
 *
 * @author slj
 * @date 2025-03-29
 */
public class CartItem implements Serializable {

    /**
     * 水果
     */
    private Fruit fruit;
    /**
     * 数量
     */
    private int quantity;

    public CartItem(Fruit fruit, int quantity) {
        if (quantity < 0) {
            throw new IllegalArgumentException("数量不能为负数‌");
        }
        this.fruit = Objects.requireNonNull(fruit, "购买水果不能为空");
        this.quantity = quantity;
    }

    /**
     * 条目总价
     *
     * @return 总价
     */
    public BigDecimal getCartItemTotal() {
        return fruit.calculateTotal(quantity);
    }

}
